{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "MyujzrAv2Vpk"
      },
      "source": [
        "##### Copyright 2020 The TensorFlow Authors."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "rTUqXTqa2Vpm"
      },
      "outputs": [],
      "source": [
        "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "rNnfCHh82Vpq"
      },
      "source": [
        "# TensorFlow Addons Callbacks: TQDM Progress Bar"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4qrDJoTw2Vps"
      },
      "source": [
        "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://www.tensorflow.org/addons/tutorials/tqdm_progress_bar\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />View on TensorFlow.org</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/addons/blob/master/docs/tutorials/tqdm_progress_bar.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://github.com/tensorflow/addons/blob/master/docs/tutorials/tqdm_progress_bar.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
        "  </td>\n",
        "      <td>\n",
        "    <a href=\"https://storage.googleapis.com/tensorflow_docs/addons/docs/tutorials/tqdm_progress_bar.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n",
        "  </td>\n",
        "</table>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "bVS_PkvX2Vpt"
      },
      "source": [
        "## Overview\n",
        "This notebook will demonstrate how to use TQDMCallback in TensorFlow Addons."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "sRldODz32Vpu"
      },
      "source": [
        "## Setup"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "H0yZwcvcR4Gc"
      },
      "outputs": [],
      "source": [
        "!pip install -U tensorflow-addons"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "etYr-Suo4KYj"
      },
      "outputs": [],
      "source": [
        "!pip install -q \"tqdm>=4.36.1\"\n",
        "\n",
        "import tensorflow as tf\n",
        "import tensorflow_addons as tfa\n",
        "\n",
        "from tensorflow.keras.datasets import mnist\n",
        "from tensorflow.keras.models import Sequential\n",
        "from tensorflow.keras.layers import Dense, Dropout, Flatten"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "SfXA0mI13pSE"
      },
      "outputs": [],
      "source": [
        "import tqdm\n",
        "\n",
        "# quietly deep-reload tqdm\n",
        "import sys\n",
        "from IPython.lib import deepreload \n",
        "\n",
        "stdout = sys.stdout\n",
        "sys.stdout = open('junk','w')\n",
        "deepreload.reload(tqdm)\n",
        "sys.stdout = stdout\n",
        "\n",
        "tqdm.__version__"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "2RGuwIwe2Vp7"
      },
      "source": [
        "## Import and Normalize Data"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "qKfrsOSP2Vp8"
      },
      "outputs": [],
      "source": [
        "# the data, split between train and test sets\n",
        "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n",
        "# normalize data\n",
        "x_train, x_test = x_train / 255.0, x_test / 255.0"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ORtL0s4X2VqB"
      },
      "source": [
        "## Build Simple MNIST CNN Model"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "z8uAGGV32VqC"
      },
      "outputs": [],
      "source": [
        "# build the model using the Sequential API\n",
        "model = Sequential()\n",
        "model.add(Flatten(input_shape=(28, 28)))\n",
        "model.add(Dense(128, activation='relu'))\n",
        "model.add(Dropout(0.2))\n",
        "model.add(Dense(10, activation='softmax'))\n",
        "\n",
        "model.compile(optimizer='adam',\n",
        "              loss = 'sparse_categorical_crossentropy',\n",
        "              metrics=['accuracy'])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YWOnH1ga2VqF"
      },
      "source": [
        "## Default TQDMCallback Usage"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Vl_oj_OW2VqG"
      },
      "outputs": [],
      "source": [
        "# initialize tqdm callback with default parameters\n",
        "tqdm_callback = tfa.callbacks.TQDMProgressBar()\n",
        "\n",
        "# train the model with tqdm_callback\n",
        "# make sure to set verbose = 0 to disable\n",
        "# the default progress bar.\n",
        "model.fit(x_train, y_train,\n",
        "          batch_size=64,\n",
        "          epochs=10,\n",
        "          verbose=0,\n",
        "          callbacks=[tqdm_callback],\n",
        "          validation_data=(x_test, y_test))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "uFvBfwJN2VqK"
      },
      "source": [
        "**Below is the expected output when you run the cell above**\n",
        "![TQDM Progress Bar Figure](https://raw.githubusercontent.com/tensorflow/addons/59961669a0e21eb4c045d4ad38d008a529d566c2/docs/tutorials/assets/tqdm_progress_bar_demo.png)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Np3dD8bhe10E"
      },
      "outputs": [],
      "source": [
        "# TQDMProgressBar() also works with evaluate()\n",
        "model.evaluate(x_test, y_test, batch_size=64, callbacks=[tqdm_callback], verbose=0)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "36WRBMo7e10I"
      },
      "source": [
        "**Below is the expected output when you run the cell above**\n",
        "![TQDM Evaluate Progress Bar Figure](https://user-images.githubusercontent.com/20843596/79412655-a57cef00-7fc3-11ea-9bea-ade8ee8dad58.PNG)"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "name": "tqdm_progress_bar.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
